package com.janetfilter.core;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author GuiBinWu
 * @Date 2024/12/18 下午 02:19
 */
public class TestProxy {

    interface Man {
        void eat();
    }

    static class WhiteMan implements Man {
        @Override
        public void eat() {
            System.out.println("白人吃饭");
        }
    }

    public static void main(String[] args) {
        WhiteMan whiteMan = new WhiteMan();
        Object o = Proxy.newProxyInstance(TestProxy.class.getClassLoader(),
                new Class[]{Man.class}, new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("我是第一层代理" + method);
                        return method.invoke(whiteMan, args);
                    }
                });
//        Man proxyWhiteMan = (Man) o;
//        proxyWhiteMan.eat();
        Object twiceProxy = Proxy.newProxyInstance(TestProxy.class.getClassLoader(),
                new Class[]{Man.class}, new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                        System.out.println("我是第二层代理" + method);
                        return method.invoke(o, args);
                    }
                });
        Man twiceProxy1 = (Man) twiceProxy;
        twiceProxy1.eat();
    }

}
